<?php

/**
 * 
 */
class MenuAdminController extends WcmController {
	
	public function actionShowAll()
	{
		$widgetType = WidgetType::model()->with('instances')->find("class=:class", array('class'=>'menu.widgets.MenuWidget'));
		$this->render('showall', array('menus'=>$widgetType->instances));
	}
	
	public function actionShowMenu($menuId)
	{
		Yii::import('menu.models.Menu');
		$menu = Menu::model()->with('items')->find("t.id=:id", array('id'=>$menuId));
		$this->render('showMenu', array('menu'=>$menu));	
	}
	
	public function actionAdd()
	{
		Yii::import('menu.models.Menu');
		$menu = new Menu();
		$menu->name = 'SecondMenu';
		$widgetType = WidgetType::model()->find("class=:class", array('class'=>'menu.widgets.MenuWidget'));
		$menu->widgetType = $widgetType;
		$menu->htmlclass = 'htmlclass';
		$menu->save();
	}
	
	public function actionUpdateMenu(){
		Yii::import('menu.models.MenuItem');
		$model = MenuItem::model();
		$transaction=$model->dbConnection->beginTransaction();
		$order = $_POST['order'];
		
		try{
			foreach ($order as $i => $id) {
				$menuItem = MenuItem::model()->findByPk($id);
				$menuItem->sequence_number=$i;
				$menuItem->save();
			}
			
			//$transaction->createCommand()->execute();
			$transaction->commit();
		}
			catch(Exception $e)
		{
    		$transaction->rollBack();
			echo $e;
		}
		
	}
}
